package com.shu.thread;


import lombok.extern.slf4j.Slf4j;

/**
 * await wait sleep yield join
 */
@Slf4j
public class ThreadTest {


    public static void main(String[] args) throws InterruptedException {
        ThreadTest threadTest = new ThreadTest();
        threadTest.waitNotify();


        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("子线程启动执行完毕");
        });
        thread.start();
        sleep();
//        thread.yield();

//        thread.join();

        log.info("主线程执行完毕");
    }

    private static void sleep() throws InterruptedException {
        Thread.sleep(2000);
        log.info("sleep 2 秒种 完毕");
    }

    private void waitNotify() throws InterruptedException {
        Object a = new Object();

        new Thread(() -> {
            synchronized (a) {
                log.info("a start");
                try {
                    a.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("a end");
            }
        }).start();


        new Thread(() -> {
            synchronized (a) {
                log.info("b start");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                a.notify();
                log.info("b end");
            }
        }).start();
        log.info(" waitNotify ----- end");
    }
}
